package cn.bdqn.serlet;

import cn.bdqn.entity.Blog;
import cn.bdqn.entity.User;
import cn.bdqn.entity.page;
import cn.bdqn.service.BlogService;
import cn.bdqn.service.impl.BlogServiceImpl;
import cn.bdqn.service.impl.pageServiceImpl;
import cn.bdqn.service.pageService;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/pageServlet")
public class pageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        pageService service = new pageServiceImpl();
        page page = new page();
        //设置页面大小
        page.setPageSize(5);
        //设置总条数,总页数不需要单独设置了,因为在setTotalCount方法中已经对总页数进行了设置
        page.setTotalCounts(service.getTotalCounts());
        //设置当前页数
        String currentStr = request.getParameter("currentPageNo");
        int currentPageNo = 1;
        if(currentStr!=null && currentStr.length()>0){
            currentPageNo = Integer.parseInt(currentStr);
        }
        page.setCurrentPageNo(currentPageNo);//当前页码数应该是从前台传过来的,动态值


        HttpSession session = request.getSession();
        ServletContext application= session.getServletContext();
        response.setCharacterEncoding("UTF-8");
        BlogService blogService=new BlogServiceImpl();
        User user= (User) application.getAttribute("user");
        String opr=request.getParameter("opr");
        if("hoem".equals(opr)) {
            application.removeAttribute("list");
            String text=request.getParameter("textfield");
            List<Blog> list=blogService.getPersonal_Information(-1,text);
            application.setAttribute("list", list);
            response.sendRedirect("home.jsp?opr=hoem");
        }else if("mywbc".equals(opr)){
            application.removeAttribute("list");
            String text1=(String)session.getAttribute("text");
            List<Blog> list=blogService.getPersonal_Information((int)user.getId(),text1);
            application.setAttribute("list",list);
            response.sendRedirect("mywbc.jsp");
        }else if("index".equals(opr)){
            application.removeAttribute("list");
            String textfield1=request.getParameter("textfield1");
            List<Blog> list=blogService.getPersonal_Information(-1,textfield1);
            application.setAttribute("list", list);
            response.sendRedirect("index.jsp");
        }
    }
}
